#!/bin/bash
mysql_yum_address="http://repo.mysql.com/mysql57-community-release-el7-11.noarch.rpm"
mysql_yum_address="http://repo.mysql.com/mysql57-community-release-el7.rpm"
base_address="https://repo.mysql.com/yum/mysql-5.7-community/el/7/x86_64/"
MCC="mysql-community-common-5.7.23-1.el7.x86_64.rpm"
MCD="mysql-community-libs-5.7.23-1.el7.x86_64.rpm"
MCS="mysql-community-server-5.7.23-1.el7.x86_64.rpm"
MCL="mysql-community-client-5.7.23-1.el7.x86_64.rpm"
MCE="mysql-community-embedded-5.7.23-1.el7.x86_64.rpm"
mysql_yum_install(){
# 手动安装自定版本
for i in ($MCC $MCD $MCS $MCL $MCE)
do
wget -P /tar $base_address/$i
done
# 添加mysql yum 存储库
yum localinstall mysql57-community-release-el7-11.noarch.rpm
# 安装特定版本的mysql
#yum-config-manager --disable mysql57-community
#yum-config-manager --enable mysql56-community
# 手动编辑 mysql repo文件安装
tee -a /etc/yum.repos.d/mysql-community.repo <<-EOF
[mysql57-community]
name=MySQL 5.7 Community Server
baseurl=http://repo.mysql.com/yum/mysql-5.7-community/el/7/$basearch/
enabled=1
gpgcheck=1
gpgkey=file:///etc/pki/rpm-gpg/RPM-GPG-KEY-mysql
EOF
# 安装mysql
yum install mysql-community-server
service mysqld start
chkconfig mysqld on
# 修改账号密码
pasword=`grep 'temporary password' /var/log/mysqld.log`
mysql -uroot -p 
ALTER USER 'root'@'localhost' IDENTIFIED BY 'MyNewPass4!';
}
mysql_binary_install(){
# 创建mysql 安装目录
mkdir /usr/local/mysql
mkdir -p /data/mysql
mkdir -p /var/lib/mysql/log
mkdir /var/lib/mysql/tmp
cd /usr/local
groupadd mysql
userradd -r -g mysql -s /bin/false mysql
chown -R mysql:mysql mysql
chown mysql. -R /data/mysql
chown mysql. -R /usr/local/mysql
# 下载mysql 二进制包
wget -P /tar  https://cdn.mysql.com//Downloads/MySQL-5.7/mysql-5.7.23-linux-glibc2.12-x86_64.tar.gz
tar zxf mysql-5.7.23-linux-glibc2.12-x86_64.tar.gz -C /usr/local/src
mv /usr/local/src/mysql-5.7.23-linux-glibc2.12-x86_64/* /usr/local/mysql
cp /usr/local/mysq/support-files/mysql.server /etc/init.d/mysql
sed -i 's@basedir=@basedir=/usr/local/mysql@g' /etc/init.d/myql
sed -i 's@datadir=@basedir=/var/lib/mysql@g' /etc/init.d/mysql
# 创建默认配置文件
tee -a /etc/my.cnf <<-EOF
[client]
port=3306
defautl-character-set=utf8

[mysqld]
basedir=/usr/local/mysql
datadir=/data/mysql
socket=/var/lib/mysql/mysql.sock
symbolic-links=0
port=3306
user=mysql
log-error=/var/lib/mysql/log/error.log
tmpdir=/var/lib/mysql/tmp
character-set-server=utf8
default_storage_engine = InnoDB
max_connections=20000
slave_skip_errors=all
slow_query_log = 1
sql_mode=STRICT_TRANS_TABLES,NO_ZERO_IN_DATE,NO_ZERO_DATE,ERROR_FOR_DIVISION_BY_ZERO,NO_AUTO_CREATE_USER,NO_ENGINE_SUBSTITUTION
EOF
# 初始化mysql
/usr/local/mysql/bin/mysqld --default-file=/etc/my.cnf --initialize --user=mysql --basedir=/usr/local/mysql --datadir=/data/mysql
# 配置环境变量
echo "export PATH=$PATH:/usr/local/mysql/bin" >> /etc/profile
source /etc/profile
# 启动mysql
service mysqld start
}
case $1 in
mysql-yum)
	mysql_yum_install
	;;
mysql-binary)
	mysql_binary_install
	;;
*)
	echo "Usge:请选择mysql安装方式(mysql-yum|mysql-binary)!"
esac
service status mysqld | grep active
if [ $? -eq 0 ];then
	echo "mysql installing is ok!"
	mysql_version=`mysql -V | awk '{print $3}'`
	printf "mysql install version is %s" $mysql_version
fi